﻿using AutoMapper;
using Domain.Factory_Modeling;
using Domain.Material_Management;
using Infratructure.Interface;
using MediatR;
using ReturnValue;

namespace MES.Server.API.Application.Command.Material_Management
{
    public class DelMaterialGroupHander : IRequestHandler<DelMaterialGroupCommand, ApiResult<int>>
    {
        private readonly IRepository<MaterialGroupModel> materialGroupRepo;
        private readonly IMapper mapper;
        public DelMaterialGroupHander(IRepository<MaterialGroupModel> materialGroupRepo, IMapper mapper)
        {
            this.materialGroupRepo = materialGroupRepo;
            this.mapper = mapper;
        }
        /// <summary>
        /// 删除物料群组
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(DelMaterialGroupCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<int>();
            // 批删逻辑
            if (request.Ids != null && request.Ids.Any())
            {
                var MaterialGroups = new List<MaterialGroupModel>();
                var notFoundIds = new List<long>();

                foreach (var id in request.Ids)
                {
                    var MaterialGroup = await materialGroupRepo.GetValue(id);
                    if (MaterialGroup == null)
                    {
                        notFoundIds.Add(id);
                    }
                    else
                    {
                        MaterialGroup.IsDel = true;
                        MaterialGroups.Add(MaterialGroup);
                    }
                }

                if (MaterialGroups.Any())
                {
                    await materialGroupRepo.UpdateRangeAsync(MaterialGroups);
                    result.Code = StatusCode.Success;
                    result.Message = $"成功删除{MaterialGroups.Count}个物料群组";
                    result.Data = MaterialGroups.Count;

                    if (notFoundIds.Any())
                    {
                        result.Message += $",以下物料群组不存在: {string.Join(",", notFoundIds)}";
                    }
                }
                else
                {
                    result.Code = StatusCode.Fail;
                    result.Message = "没有找到可删除的物料群组信息";
                }
            }
            else
            {
                var materialGroup = await materialGroupRepo.GetValue(request.Id);
                if (materialGroup == null)
                {
                    result.Code = StatusCode.Fail;
                    result.Message = "物料群组不存在,无法删除";
                    return await Task.FromResult(result);
                }
                else
                {
                    materialGroup.IsDel = true;
                    await materialGroupRepo.UpdateAsync(materialGroup);
                    result.Code = StatusCode.Success;
                    result.Message = "删除物料群组成功";
                    return await Task.FromResult(result);
                }
            }
            return await Task.FromResult(result);
            
        }
    }
}
